#include "MatrixA.h"


void MatrixA::recompose(int i){
     FileManager* output = new FileManager();
     output->openFile("./fileAux/matriz.txt", "out");
     FileManager* input[i];
     for (int j = 0; j < i; j++){
         input[j]=new FileManager();
         input[j]->openFile("./fileAux/"+StringManager::int2string(j)+".frec", "in");
     }
     int k = 0;
     while (!input[0]->eof()){
           string strIn = input[k]->readLine();
           if (strIn != ""){
              if (k < i-1){
                 output->write(strIn + "\t");
                 k++;
              }
              else{
                   output->writeLine(strIn);
                   k = 0;
              }
           }
     }
     for (int j = i-1; j >= 0; j--){
         input[j]->closeFile();
         input[j]->deleteFile();
         delete input[j];
     }
     
     output->closeFile();
     delete output;
}


void MatrixA::transform(int autovalores){
     FileManager* input = new FileManager();


     input->openFile("./fileAux/matriz.txt", "in");

     
     FileManager* output = new FileManager();
     output->openFile("./fileAux/matrizA.txt", "out");

     while (!input->eof()){
           string strIn = input->readLine();
           
           string copy = strIn;
           double gi = MatrixA::entropy(copy);
           while (strIn.length() > 0){
                 string word = StringManager::splitByValue(strIn, &strIn, '\t');
                 double tij = StringManager::string2num(word);

                 double lij;
                 if(autovalores <= 20)
                	 lij = MatrixA::binary(tij);
                 else
                	 if(autovalores <=50)
                	 	lij = MatrixA::log(tij);
                	 else
                	 	lij = MatrixA::tf(tij);


                 output->write(StringManager::num2string(lij*gi));
                 output->write((strIn.length() > 0)?" ":"\n");
           }
     }
     
     input->closeFile();
     input->deleteFile();
     delete input;
     output->closeFile();
     delete output;   
}

double MatrixA::log(double tij){
       return log10(1+tij);
}

double MatrixA::tf(double tij){
       return tij;
}

double MatrixA::binary(double tij){
       return (tij != 0)?1:0;
}

double MatrixA::entropy(string str){
       string frq = str;
       double gfi = 0;
       while (frq.length() > 0){
             string auxGfi = StringManager::splitByValue(frq, &frq, '\t');
             gfi += StringManager::string2num(auxGfi);
       }
       double gi = 0;
       double n = 0;
       double j = 0;
           
       while (str.length() > 0){
             string word = StringManager::splitByValue(str, &str, '\t');
             double tij = StringManager::string2num(word);
             if (tij != 0){
                double pij = tij/gfi;
                gi += pij*log10(pij);
                j++;
             }
             n++;
       }
       gi = gi/(j*log10(n));
       gi++;
       return gi;
}

double MatrixA::idf(string str){
       double n = 0;
       double dfi = 0;
           
       while (str.length() > 0){
             string word = StringManager::splitByValue(str, &str, '\t');
             double tij = StringManager::string2num(word);
             if (tij != 0){
                dfi++;
             }
             n++;
       }
       double gi = log10(n/(1+dfi))/log10(2);
       return gi;
}

double MatrixA::normal(string str){
       string frq = str;
       double tij = 0;
       while (str.length() > 0){
             string auxTij = StringManager::splitByValue(str, &str, '\t');
             tij += pow(StringManager::string2num(auxTij),2);
       }
       double gi = 1/sqrt(tij);
       return gi;
}


